package list2;

public class MysingleList implements IList{

    // public ListNode last;  //链表的尾节点
    public ListNode head;   //链表的头节点-->链表的属性
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    @Override
    public void addLast(int data) {
        //使用尾插法时要注意判断链表是否为空
        ListNode node = new ListNode(data);

        if(head==null){
            head = node;
        }else {
            ListNode cur = head;
            while (cur.next!=null){//while(cur!=null)
                cur = cur.next;
            }
            cur.next = node;//cur=node;
        }


    }

    @Override
    public void addIndex(int index, int data) throws IndexException{
        //cur节点如果已经指向后一个节点,那么是无法指向前一个节点的

        //1.错误解法
        /*ListNode node = new ListNode(data);
        int flag = 0;
        ListNode cur = head;
        while(cur!=null){
            flag++;
            cur=cur.next;
        }
        if(index>flag){
            System.out.println("超出数组范围!");
        }else{
            for(int i=0;i<flag;i++){
                if(i==index){
                    node.next = cur.next;
                    cur.next = node;
                }
            }
        }*/

        //2.正确解法
        if(index<0||index>size()){
            throw new IndexException("index不合法"+index);
        }
        ListNode node = new ListNode(data);
        if(head==null){
            head=node;
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){//index是要插入的下标位置  size是整个链表的长度
            addLast(data);
            return;
        }
        //中间插入
        ListNode cur = searchPrevIndex(index);//返回index前一个目标节点
        node.next=cur.next;
        cur.next=node;
    }

    private ListNode searchPrevIndex(int index){
        ListNode cur=head;
        int count=0;
        while(count!=index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur!=null){
            if(cur.value==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //只移掉第一个
        if(head==null){
            return;
        }
        if(head.value==key){
            head=head.next;
            return;
        }
        ListNode cur = findPrevKey(key);
        cur.next = cur.next.next;
    }

    private ListNode findPrevKey(int key) {
        ListNode cur = head;
        while (cur.next!=null){
            if(cur.next.value==key){
                break;
            }
            cur=cur.next;
        }
        return cur;
    }

    @Override
    public void removeAIIKey(int key) {
        //删除所有关键字的节点
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur!=null){
            //因为要对比所有节点的value,所以要遍历所有节点(cur!=null),如果使用(cur.next!=null),则最后一个节点无法进行比较
            if(cur.value==key){
                prev.next = cur.next;
                cur=cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
    }

    @Override
    public int size() {
        int i=0;
        ListNode cur = head;
        while(cur!=null){
            i++;
            cur = cur.next;
        }
        return i;
    }

    @Override
    public void clear() {
        /*while(head!=null) {
            System.out.println(head.value + " ");
            head = head.next;
        }*/
        head=null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.value+" ");
            cur = cur.next;
        }
    }

    public ListNode reverList(){
        //用来翻转链表
        if(head==null){
            return head;
        }
        if (head.next==null){
            return head;
        }
        ListNode cur = head.next;
        head.next=null;

        while(cur!=null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    public ListNode middleNode(){
        //寻找中间节点,如果有两个中间节点,那么返回第二个中间节点
        ListNode fast = head;
        ListNode slow =head;

        /*//获取链表长度
        int i=0;
        ListNode cur = head;
        while(cur!=null){
            i++;
            cur = cur.next;
        }
        if(i%2==0){//链表长度为双数
            while(fast!=null){
                fast = fast.next.next;
                slow = slow.next;
            }
        }else{
            while (fast.next!=null){
                fast = fast.next.next;
                slow = slow.next;
            }
        }*/

        while(fast!=null&&fast.next!=null){//二者不能互换,条件不一样
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }

    public ListNode KeyNode(int key) throws KthTotailException{
        //输出链表中倒数的第K个结点
        if(key<=0){
            throw new KthTotailException("关键字不合法");
        }
        if(head==null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = 0;
        while(count!=key-1){
            if(fast.next!=null){
                fast=fast.next;
                count++;
            }else{//用来判断key过大
                throw new KthTotailException("关键字不合法");
            }
        }
        while(fast.next!=null){
            fast = fast.next;
            slow  =slow.next;
        }
        return slow;
    }

    //创建一个链表
    static class ListNode{
        public int value;
        public ListNode next;//让next的类型为ListNode
        public ListNode prev;
        public ListNode(int value){//构造方法
            this.value =value;
        }
    }//每一个节点为一个内部类


}
